<template>
    <div class="container">
        <!-- 顶部状态栏 -->
        <div class="statusBar" :style="{ height: statusBarHeight + 'px' }"></div>
        <!-- 顶部导航栏 -->
        <div class="navBar" :style="{ height: 88 + 'rpx' }">
            <img :src="imgs.back" @click="back" />
            缴费查询
        </div>

        <!-- 输入区域 -->
        <div class="plate-container">
            <div class="plate-input">
                <div class="plate-input-content">
                    <div class="plate-input-label">请输入车牌号码</div>
                    <div class="plate-input-list">
                        <div class="plate-input-item" :class="{ current: index === inputIndex, dashed: index === 7 }" v-for="(item, index) in numberArray" :key="index">
                            {{ item }}
                            <div v-if="index === 7 && !item">新能源</div>
                        </div>
                    </div>
                    <button class="plate-input-button" @click="confirm">查询</button>
                </div>
            </div>
        </div>

        <!-- 键盘区域 -->
        <div class="plate-content">
            <!-- 数字键盘 -->
            <template v-if="plateIndex === 0">
                <div class="plate-row">
                    <button class="plate-btn" v-for="item in toArray(row1)" :key="item" @click="() => select(item)">{{ item }}</button>
                </div>
                <div class="plate-row">
                    <button class="plate-btn" v-for="item in toArray(row2)" :key="item" @click="() => select(item)">{{ item }}</button>
                </div>
                <div class="plate-row">
                    <button class="plate-btn" v-for="item in toArray(row3)" :key="item" @click="() => select(item)">{{ item }}</button>
                </div>
                <div class="plate-row">
                    <button class="plate-btn plate-btn-gray" @click="plateIndex = 1">地区</button>
                    <button class="plate-btn" v-for="item in toArray(row4)" :key="item" @click="() => select(item)">{{ item }}</button>
                    <button class="plate-btn plate-btn-gray" @click="() => backspace()">
                        <img :src="imgs.backspace" />
                    </button>
                </div>
            </template>
            <!-- 地区键盘 -->
            <template v-else-if="plateIndex === 1">
                <div class="plate-row">
                    <button class="plate-btn" v-for="item in toArray(row5)" :key="item" @click="() => select(item)">{{ item }}</button>
                </div>
                <div class="plate-row">
                    <button class="plate-btn" v-for="item in toArray(row6)" :key="item" @click="() => select(item)">{{ item }}</button>
                </div>
                <div class="plate-row">
                    <button class="plate-btn" v-for="item in toArray(row7)" :key="item" @click="() => select(item)">{{ item }}</button>
                </div>
                <div class="plate-row">
                    <button class="plate-btn plate-btn-gray" @click="plateIndex = 0">ABC</button>
                    <button class="plate-btn" v-for="item in toArray(row8)" :key="item" @click="() => select(item)">{{ item }}</button>
                    <button class="plate-btn plate-btn-gray" @click="() => backspace()">
                        <img :src="imgs.backspace" />
                    </button>
                </div>
            </template>
        </div>
    </div>
</template>
<script>
import { getOrderDetailByCarNumberAPI } from "../api/api";

const app = getApp();
export default {
    data() {
        return {
            imgs: app.globalData.imgs,
            visible: false,
            statusBarHeight: "",

            // 数字键盘
            row1: "1234567890",
            row2: "QWERTYUIOP",
            row3: "ASDFGHJKL",
            row4: "ZXCVBNM",

            // 地区键盘
            row5: "京沪粤津冀晋蒙辽吉黑",
            row6: "苏浙皖闽赣鲁豫鄂湘桂",
            row7: "琼渝川贵云藏陕甘青宁",
            row8: "新使领警学港澳",

            list: [],

            plateIndex: 0,
            inputIndex: 1,
            numberArray: ["湘", "", "", "", "", "", "", ""],
            loading: false,

            temp_token: "",
            pagesLength: 1,
        };
    },
    onShareAppMessage() {},
    onLoad() {
        // 获取顶部安全区域
        wx.getSystemInfo({
            success: (res) => {
                this.statusBarHeight = res.statusBarHeight;
            },
        });

        this.pagesLength = getCurrentPages().length;
    },
    onShow() {
        // 获取临时token
        wx.showLoading({
            title: "加载中...",
        });
        app.getTempToken()
            .then((res) => {
                this.temp_token = res.token;
                wx.hideLoading();
            })
            .catch((err) => {
                wx.hideLoading();
            });
    },
    methods: {
        back() {
            if (this.pagesLength > 1) {
                uni.navigateBack();
            } else {
                uni.switchTab({ url: "/pages/index" });
            }
        },
        toArray(string) {
            return string.split("");
        },
        // 点击键盘
        select(item) {
            this.numberArray[this.inputIndex] = item;
            if (this.inputIndex < 7) {
                this.inputIndex++;
            } else if (this.inputIndex === 7) {
                this.numberArray[this.inputIndex] = item;
                this.$forceUpdate();
            }
            if (this.inputIndex > 0) {
                this.plateIndex = 0;
            }
        },
        // 退格
        backspace() {
            this.numberArray[this.inputIndex] = "";
            if (this.inputIndex > 0) {
                this.inputIndex--;
            }
            if (this.inputIndex === 0) {
                this.plateIndex = 1;
            }
        },
        // 确定查询
        confirm() {
            if (!this.numberArray[6]) {
                wx.showToast({
                    title: "请输入完整车牌号",
                    icon: "none",
                });
                return false;
            }
            let carNumber = this.numberArray.join("");

            wx.showLoading({
                title: "查询中",
            });
            getOrderDetailByCarNumberAPI(
                {
                    plate_number: carNumber,
                    token: this.temp_token,
                },
                (err) => {
                    if ((err && err.code === 411) || err.code === 413) {
                        // 411短期token过期 413订单过期
                        wx.showToast({
                            title: `订单超时，请在页面自动刷新后重试`,
                            icon: "none",
                        });
                        setTimeout(() => {
                            const pages = getCurrentPages();
                            const page = pages[pages.length - 1];
                            uni.redirectTo({
                                url: decodeURIComponent(page.$page.fullPath),
                            });
                        }, 1500);
                    } else {
                        wx.showToast({
                            title: err ? err.msg : "未知错误",
                            icon: "none",
                        });
                    }
                    return Promise.reject(err);
                }
            ).then((res) => {
                uni.$u.route("/pages/order_detail", { carNumber });
            });
        },
    },
};
</script>
<style scoped lang="scss">
.container {
    display: flex;
    flex-direction: column;
    align-items: center;
    .statusBar {
        position: relative;
        z-index: 2;
        width: 100%;
        background-color: #197ce4;
    }
    .navBar {
        position: relative;
        z-index: 2;
        width: 100%;
        font-size: 30rpx;
        line-height: 1;
        display: flex;
        align-items: center;
        justify-content: center;
        color: #fff;
        background-color: #197ce4;
        img {
            position: absolute;
            top: 19rpx;
            left: 20rpx;
            width: 50rpx;
            height: 50rpx;
        }
    }
    .plate-container {
        width: 100%;
        background-image: linear-gradient(top, #197ce4, #ffffff);
        display: flex;
        align-items: center;
        justify-content: center;
    }
    .plate-input {
        bottom: 600rpx;
        width: 686rpx;
        border-radius: 24rpx;
        background-color: #fff;
        overflow: hidden;
        margin-top: 30rpx;
        box-shadow: 1rpx 1rpx 3rpx 1rpx #efefef;
        .plate-input-content {
            padding: 20rpx 0 50rpx;
            display: flex;
            flex-direction: column;
            align-items: center;
            .plate-input-label {
                width: 100%;
                color: #000;
                font-size: 30rpx;
                line-height: 1;
                font-weight: bold;
                margin: 20rpx 0;
                padding-left: 40rpx;
                box-sizing: border-box;
            }
            .plate-input-list {
                width: 100%;
                display: flex;
                align-items: center;
                justify-content: center;
                margin: 30rpx 0;
                .plate-input-item {
                    display: flex;
                    justify-content: center;
                    align-items: center;
                    margin: 0 8rpx;
                    width: 60rpx;
                    height: 80rpx;
                    border-radius: 10rpx;
                    border: 1rpx solid #ccc;
                    &.dashed {
                        border: 1rpx dashed #ccc;
                        div {
                            font-size: 18rpx;
                            color: #999;
                        }
                    }
                    &.current {
                        border: 1rpx solid #6794f6;
                    }
                }
            }
            .plate-input-button {
                width: 90%;
                background-color: #6794f6;
                color: #fff;
                height: 100rpx;
                border-radius: 50rpx;
                display: flex;
                justify-content: center;
                align-items: center;
                margin: 20rpx 0;
                &.disabled {
                    background-color: #999;
                }
            }
        }
    }
    .plate-content {
        width: 100%;
        position: fixed;
        left: 0;
        bottom: 0;
        background-color: #d4d7de;
        padding: 20rpx 0 50rpx;
        border-top-left-radius: 12rpx;
        border-top-right-radius: 12rpx;
        .plate-row {
            display: flex;
            justify-content: center;
            .plate-btn {
                padding: 0;
                font-size: 32rpx;
                display: flex;
                align-items: center;
                justify-content: center;
                width: 60rpx;
                height: 80rpx;
                margin: 10rpx 6rpx;
                background-color: #fff;
                color: #333;
                border-radius: 12rpx;
                border-bottom: 1rpx solid #666;
                &.plate-btn-gray {
                    width: 90rpx;
                    background-color: #b1b8c8;
                    font-size: 28rpx;
                }
                &:active {
                    transform: translateY(4rpx);
                }
                img {
                    width: 40rpx;
                    height: 40rpx;
                }
            }
        }
    }
}
</style>
